/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Classes.Imprimante;
import Classes.Materiel;
import Classes.Retroprojecteur;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author apo
 */
public class ImprimanteDAO extends DAO<Imprimante> {
       /**
     * Constructeur de la classe OrdinateurDAO
     * @param conn 
     */
    public ImprimanteDAO(java.sql.Connection con) {
        super(con);
    }
    
/**
 * La fonction prend en paramètre un objet Imprimante et insère les informations de l'objet en base
 * @param i
 * @throws SQLException 
 */
    public void insert(Imprimante i) throws SQLException {
      MaterielDAO myMaterielDAO = new MaterielDAO(connexion);
        myMaterielDAO.insert(i);
 
        String query = " INSERT INTO"
                + " imprimante"
                + " (numCB, refCC, refCN, pUSB, pParall, pRes, TYPEI, A3, A4)"
                + " VALUES ("
                +i.getNumCB()+ "," 
                + "\"" +i.getRefCC() + "\"," 
                + "\"" +i.getRefCN() + "\","
                + "\"" +i.getPortUsb() + "\"," 
                + "\"" +i.getPortParallele() + "\","
                + "\"" +i.getPortRes() + "\","
                + "\"" +i.getTypeImpression() + "\"," 
                +i.getFormatA3() + ","
                +i.getFormatA4() + ")";
        System.out.println(query);
        state.executeUpdate(query);
    }
/**
 * La fonction prend en paramètre un objet Imprimante et supprime l'entrée correspondante à l'id de l'objet 
 * @param i
 * @throws SQLException 
 */
    public void delete(Imprimante i) throws SQLException {
        String query = " DELETE FROM"
                + " imprimante"
                + " WHERE numCB ="
                + i.getNumBC();

        state.executeUpdate(query);
    }

/**
 * La fonction prend en paramètre un objet Imprimante et met a jour les informations correspondantes à l'objet 
 * @param i
 * @throws SQLException 
 */
    public void update(Imprimante i) throws SQLException {
                MaterielDAO myMaterielDAO = new MaterielDAO(connexion);
        myMaterielDAO.update(i);
  
        
        String query = " UPDATE  imprimante"
                    + " SET"
                    + "  refCC = \""    + i.getRefCC()+"\""
                    + " ,refCN = \""      + i.getRefCN()+"\""
                    + " ,pUSB = \""     + i.getPortUsb()+"\""
                    + " ,pParall = \""   + i.getPortParallele()+"\""
                    + " ,pRes = \""    + i.getPortRes()+"\""
                    + " ,typeI = \""  + i.getTypeImpression()+"\""
                    + " ,A3 = \""    + i.getFormatA3()+"\""
                    + " ,A4 = \""      + i.getFormatA4()+"\""
                    + " WHERE numCB = "+ i.getNumBC();

        state.executeUpdate(query);
    }
    /**
     * La fonction recherche toutes les imprimantes de la base de données et construit une Map d'imprimantes
     * @return Map<Long, Imprimante>
     */
     public Map <Long, Imprimante> findAll() {
             Map<Long,Imprimante>listeImprimante = new HashMap();
             ResultSet result2;
             try {
                String query = "SELECT * FROM imprimante";
                result2 = state.executeQuery(query);
                while (result2.next()) {
                     listeImprimante.put(result2.getLong("numCB"), new Imprimante(result2.getLong("numCB"), result2.getString("refCC"), result2.getString("refCN"), result2.getString("pUSB"), result2.getString("pParall"), result2.getString("pRes"), result2.getString("typeI"), result2.getString("A4"), result2.getString("A3")));   
                }  
                return listeImprimante;
                       
                        
		} 
		catch (SQLException exc) {
			System.out.println("[Debug] Problème query : ImprimanteDAO - findAll()");
			exc.printStackTrace();
                       
		}
		return null;
        }
     
     /**
      * Prend en parametre un code barre d'une imprimante et retourne toutes les caractéristiques de celle-ci
      * @param CB
      * @return object<imprimante>
      */
     public Imprimante loadByCodeBarre(Long CB){
        
        Imprimante myImprimante = new Imprimante();
        ResultSet result;
        
        try {
            String query = "SELECT * FROM imprimante I"
                        + " LEFT JOIN materiel M"
                        + " ON I.numCB = M.numCB"
                        + " WHERE M.numCB = " + CB;
            result = state.executeQuery(query);
            while (result.next()) {
                
                myImprimante.setNumCB(result.getLong("numCB"));
                myImprimante.setNumCompta(result.getInt("numCompta"));
                myImprimante.setNumSerie(result.getString("numSerie"));
                myImprimante.setNom(result.getString("nom"));
                myImprimante.setDescriptif(result.getString("descriptif"));
                myImprimante.setDateHT(result.getString("dateHT"));
                myImprimante.setPrixHT(result.getDouble("prixHT"));
                myImprimante.setGarantie(result.getInt("garantie"));
                myImprimante.setNumBC(result.getString("numBC"));
                myImprimante.setBudget(result.getString("budget"));
                myImprimante.setAmortissement(result.getDouble("amortissement"));
                myImprimante.setIdT(result.getInt("idT"));
                myImprimante.setIdM(result.getInt("idM"));
                myImprimante.setIdS(result.getInt("idS"));
                myImprimante.setIdSc(result.getInt("idSc"));
                myImprimante.setRefCC(result.getString("refCC"));
                myImprimante.setRefCN(result.getString("refCN"));
                myImprimante.setPortUsb(result.getString("pUSB"));
                myImprimante.setPortParallele(result.getString("pParall"));
                myImprimante.setPortRes(result.getString("pRes"));
                myImprimante.setTypeImpression(result.getString("TYPEI"));
                myImprimante.setFormatA3(result.getString("A3"));
                myImprimante.setFormatA4(result.getString("A4"));
                                
            }
            
        } catch (SQLException exc) {
            System.out.println("[Debug] Problème query : ImprimanteDAO - loadByCodeBarre()");
            exc.printStackTrace();
            
        }
        return myImprimante;
        
    }
}
